package com.carefree.system.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carefree.common.core.PageUtils;
import com.carefree.common.core.QueryParam;
import com.carefree.common.core.Result;
import com.carefree.common.emus.StatusCodeEnum;
import com.carefree.common.exception.BusinessException;
import com.carefree.system.common.utils.SecurityUtils;
import com.carefree.system.modules.admin.dao.LoginLogDao;
import com.carefree.system.modules.admin.entity.LoginLogEntity;
import com.carefree.system.modules.admin.service.LoginLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 登录日志
 *
 * @author tengbin
 */
@Service("loginLogService")
public class LoginLogServiceImpl extends ServiceImpl<LoginLogDao, LoginLogEntity> implements LoginLogService {

    @Resource
    private SecurityUtils securityUtils;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<LoginLogEntity> queryWrapper = new QueryWrapper<>();
        String keyword = (String) params.get("keyword");
        queryWrapper.like(StringUtils.isNotBlank(keyword), "username", keyword);

        IPage<LoginLogEntity> page = page(new QueryParam<LoginLogEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 保存登录日志
     *
     * @param username 当前登录的用户名
     * @param ipaddr   当前登录用户的ip地址
     * @param status   登录状态: 0成功; 1失败
     * @param msg      日志描述
     * @return
     */
    @Override
    public boolean saveLoginLog(String username, String ipaddr, Integer status, String msg) {
        if (StringUtils.isAnyBlank(username, ipaddr, msg) || status == null) {
            throw new BusinessException(StatusCodeEnum.ILLEGAL_REQUEST_ERROR);
        }
        LoginLogEntity loginLogEntity = new LoginLogEntity();
        loginLogEntity.setUsername(username);
        loginLogEntity.setIpaddr(ipaddr);
        loginLogEntity.setStatus(status);
        loginLogEntity.setMsg(msg);
        loginLogEntity.setAccessTime(new Date());
        loginLogEntity.setIsDeleted(0);

        return save(loginLogEntity);
    }

    /**
     * 查询当前用户的登录日志信息
     *
     * @param username 当前用户的用户名
     * @return
     */
    @Override
    public Result<List<LoginLogEntity>> queryLooginLogByUserName(String username) {
        String userName = securityUtils.getPrincipalForUserName();
        if (StringUtils.isAnyBlank(userName, username)) {
            throw new BusinessException(StatusCodeEnum.ILLEGAL_REQUEST_ERROR);
        }
        if (userName.equals(username)) {
            List<LoginLogEntity> loginlogs = list(new QueryWrapper<LoginLogEntity>().eq("username", userName).orderByDesc("create_time"));
            return Result.success(loginlogs);
        }
        return Result.error(StatusCodeEnum.ILLEGAL_TOKEN.getCode(), StatusCodeEnum.ILLEGAL_TOKEN.getMessage());
    }
}
